<template>
  <div class="ingot-statistics">
    <el-scrollbar>
      <div class="top-card-container">
        <statisticalDataCard :data="cardList" width="270px"></statisticalDataCard>
      </div>
    </el-scrollbar>

    <BasicCard style="margin-top: 20px">
      <BasicVxeTable ref="gridRef" v-bind="gridOption"></BasicVxeTable>
    </BasicCard>
    <BasicCard style="margin-top: 20px">
      <BasicVxeTable ref="gridRefShift" v-bind="gridOptionShift"></BasicVxeTable>
    </BasicCard>
    <BasicCard style="margin-top: 20px">
      <BasicVxeTable ref="gridRefMachine" v-bind="gridOptionMachine"></BasicVxeTable>
    </BasicCard>
  </div>
</template>
<script setup lang="ts">
  import statisticalDataCard from '../../../businessComponents/statisticalDataCard.vue'
  // import { MaterialRowVO } from '../types'
  import {
    MaterialTableColumns,
    shiftMaterialTableColumns,
    machineMaterialTableColumns
  } from '../../data'
  import { valueZhuaneFloat, splitTsIdToTime } from '@/utils/custom/index'

  import { getTongJiList } from '@/api/shengChanJianShi'

  const { t } = useI18n()
  const cardList = ref<any[]>([])
  // const materialData = ref<MaterialRowVO[]>([])

  // 物料报表
  const gridOption = reactive<BasicGridProps>({
    extraConfig: {
      title: t('custom.物料报表'),
      isCustom: true,
      isExport: true,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `singleIngotStatistics-material`, // id用于缓存,务必配置
    toolbarConfig: {}, // 工具栏
    maxHeight: 500,
    exportConfig: {
      filename: '数据表'
    },
    columns: MaterialTableColumns
  })
  // 班次-物料报表
  const gridOptionShift = reactive<BasicGridProps>({
    extraConfig: {
      title: t('custom.shiftMaterial'),
      isCustom: true,
      isExport: true,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `singleIngotStatistics-material-shift`, // id用于缓存,务必配置
    toolbarConfig: {}, // 工具栏
    maxHeight: 500,
    exportConfig: {
      filename: '数据表'
    },
    columns: shiftMaterialTableColumns
  })
  // 机台-班次-物料报表
  const gridOptionMachine = reactive<BasicGridProps>({
    extraConfig: {
      title: t('custom.machineShiftMaterial'),
      isCustom: true,
      isExport: true,
      isFilterAndSort: true
    }, // 拓展的配置
    id: `singleIngotStatistics-material-shift-machine`, // id用于缓存,务必配置
    toolbarConfig: {}, // 工具栏
    maxHeight: 500,
    exportConfig: {
      filename: '数据表'
    },
    columns: machineMaterialTableColumns
  })

  const getAllDataList = () => {
    gridOption.loading = true
    gridOptionShift.loading = true
    gridOptionMachine.loading = true
    getTongJiList({
      materialIds: [
        '09b917ab-7534-91ab-a16f-3a16f89cc02f',
        'cc333409-6143-bd0b-0a79-3a08d1b6f091',
        '38290b10-d438-c440-c311-3a0be9b991a9',
        '7945a58c-1862-3974-e009-3a19d0990766',
        '9e5f7175-a0cf-3e2e-4bb1-3a0aa4d5fe8a',
        '7a787f43-6a67-9210-593e-3a0bb033ee24'
      ],
      tsIdEnd: '25070302',
      tsIdStart: '25070302'
    }).then((res: any) => {
      gridOption.loading = false
      gridOptionShift.loading = false
      gridOptionMachine.loading = false
      // 顶部卡片数据
      cardList.value = handleFormatCardData(res.positionStatisticsByMaterial)
      // 物料报表数据
      gridOption.data = res.positionStatisticsByMaterial
        .sort((a, b) => {
          return b.efcRun - a.efcRun
        })
        .map((item) => {
          valueZhuaneFloat(item)
          item.jointTimeAvg =
            item.jointTimeAvg == null
              ? item.jointTimeAvg
              : Math.round(Number(item.jointTimeAvg) / 60)
          return item
        })

      // 班次-物料报表数据
      gridOptionShift.data = res.positionStatisticsByTsidMaterial
        .sort((a, b) => {
          return b.machineCntByMaterial - a.machineCntByMaterial
        })
        .map((item) => {
          valueZhuaneFloat(item)
          item.jointTimeAvg =
            item.jointTimeAvg == null
              ? item.jointTimeAvg
              : Math.round(Number(item.jointTimeAvg) / 60)
          return item
        })

      // 机台-班次-物料报表数据
      gridOptionMachine.data = res.positionStatisticsByMachineTsidMaterial
        .sort((a, b) => {
          return a.machineName - b.machineName
        })
        .map((item) => {
          valueZhuaneFloat(item)
          item.jointTimeAvg =
            item.jointTimeAvg == null
              ? item.jointTimeAvg
              : Math.round(Number(item.jointTimeAvg) / 60)
          item.banCi = splitTsIdToTime(item.tsid) + ' ' + item.shiftName
          return item
        })
    })
  }

  const handleFormatCardData = (data) => {
    const result = data.map((item) => {
      return {
        title: item.materialName,
        content: [
          {
            label: t('custom.产量损失累计'),
            value: parseFloat(Number(item.breakWeight).toFixed(2)),
            unit: 'kg'
          },
          {
            label: t('custom.平均巡回接头时间'),
            value: Math.floor(item.jointTimeAvg / 60),
            unit: '分钟'
          },
          {
            label: t('custom.累计断头数'),
            value: item.breakCnt,
            unit: '个'
          },
          {
            label: t('custom.累计接头数'),
            value: item.jointCnt,
            unit: '个'
          },
          {
            label: t('custom.累计落纱次数'),
            value: item.dofferCnt,
            unit: '次'
          }
        ]
      }
    })
    return result
  }

  onMounted(() => {
    getAllDataList()
  })
</script>
<style scoped lang="scss"></style>

<!-- -->
